/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.arrow.adbc.driver.jdbc.postgresql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import org.apache.arrow.adbc.core.AdbcDatabase;
import org.apache.arrow.adbc.core.AdbcDriver;
import org.apache.arrow.adbc.core.AdbcException;
import org.apache.arrow.adbc.driver.jdbc.JdbcDriver;
import org.apache.arrow.adbc.driver.jdbc.StandardJdbcQuirks;
import org.apache.arrow.adbc.driver.testsuite.SqlTestUtil;
import org.apache.arrow.adbc.driver.testsuite.SqlValidationQuirks;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.vector.types.TimeUnit;
import org.junit.jupiter.api.Assumptions;

public class PostgresqlQuirks extends SqlValidationQuirks {
  static final String POSTGRESQL_URL_ENV_VAR = "ADBC_JDBC_POSTGRESQL_URL";
  static final String POSTGRESQL_USER_ENV_VAR = "ADBC_JDBC_POSTGRESQL_USER";
  static final String POSTGRESQL_PASSWORD_ENV_VAR = "ADBC_JDBC_POSTGRESQL_PASSWORD";
  static final String POSTGRESQL_DATABASE_ENV_VAR = "ADBC_JDBC_POSTGRESQL_DATABASE";

  String catalog = "postgres";

  static String makeJdbcUrl() {
    final String postgresUrl = System.getenv(POSTGRESQL_URL_ENV_VAR);
    final String user = System.getenv(POSTGRESQL_USER_ENV_VAR);
    final String password = System.getenv(POSTGRESQL_PASSWORD_ENV_VAR);

    if (SqlTestUtil.isCI() && (postgresUrl == null || postgresUrl.isEmpty())) {
      throw new RuntimeException("PostgreSQL not found, set " + POSTGRESQL_URL_ENV_VAR);
    }

    Assumptions.assumeFalse(
        postgresUrl == null, "PostgreSQL not found, set " + POSTGRESQL_URL_ENV_VAR);
    Assumptions.assumeFalse(
        postgresUrl.isEmpty(), "PostgreSQL not found, set " + POSTGRESQL_URL_ENV_VAR);
    return String.format("jdbc:postgresql://%s?user=%s&password=%s", postgresUrl, user, password);
  }

  public Connection getJdbcConnection() throws SQLException {
    return DriverManager.getConnection(makeJdbcUrl());
  }

  @Override
  public AdbcDatabase initDatabase(BufferAllocator allocator) throws AdbcException {
    String url = makeJdbcUrl();

    final String catalog = System.getenv(POSTGRESQL_DATABASE_ENV_VAR);
    if (SqlTestUtil.isCI() && catalog == null) {
      throw new RuntimeException(
          "PostgreSQL catalog not found, set " + POSTGRESQL_DATABASE_ENV_VAR);
    }
    Assumptions.assumeFalse(
        catalog == null, "PostgreSQL catalog not found, set " + POSTGRESQL_DATABASE_ENV_VAR);
    this.catalog = catalog;

    final Map<String, Object> parameters = new HashMap<>();
    AdbcDriver.PARAM_URI.set(parameters, url);
    parameters.put(JdbcDriver.PARAM_JDBC_QUIRKS, StandardJdbcQuirks.POSTGRESQL);
    return new JdbcDriver(allocator).open(parameters);
  }

  @Override
  public void cleanupTable(String name) throws Exception {
    try (final Connection connection1 = DriverManager.getConnection(makeJdbcUrl())) {
      try (Statement statement = connection1.createStatement()) {
        statement.execute("DROP TABLE " + name);
      } catch (SQLException ignored) {
      }
    }
  }

  @Override
  public String defaultCatalog() {
    return catalog;
  }

  @Override
  public String defaultDbSchema() {
    return "public";
  }

  @Override
  public String caseFoldTableName(String name) {
    return name.toLowerCase();
  }

  @Override
  public String caseFoldColumnName(String name) {
    return name.toLowerCase();
  }

  @Override
  public TimeUnit defaultTimeUnit() {
    return TimeUnit.MICROSECOND;
  }

  @Override
  public TimeUnit defaultTimestampUnit() {
    return TimeUnit.MICROSECOND;
  }

  @Override
  public boolean supportsCurrentCatalog() {
    return true;
  }
}
